package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IWheelDetailDAO;
import com.AAL.Model.WheelDetail;
import com.AAL.Model.StatusOfContract;
import com.AAL.Model.TypesOfWheel;
import com.AAL.Model.WheelDetail;
import com.AAL.Utils.HibernateUtil;

public class WheelDetailDAO implements IWheelDetailDAO {

	@Override
	public WheelDetail saveAndUpdateWheelDetail(WheelDetail wheelDetail) {
		System.out.println("saveAndUpdateWheelDetail");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(wheelDetail);
			System.out.println("hamsave");
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return wheelDetail;
	}

	@Override
	public WheelDetail deleteWheelDetail(String wheelDetailID) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public WheelDetail getWheelDetail(String wheelDetailID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		WheelDetail wheelDetail = (WheelDetail) session.load(WheelDetail.class,
				wheelDetailID);
		return wheelDetail;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WheelDetail> getLstWheelDetailsByTypeOfWheel(TypesOfWheel type) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<WheelDetail> lstWheelDetails = null;
		try {
			lstWheelDetails = (List<WheelDetail>) session
					.createQuery(
							"from WheelDetail where typesOfWheel.typeOfWheelID=:typeOfWheelID")
					.setParameter("typeOfWheelID", type.getTypeOfWheelID())
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstWheelDetails;
	}

	@Override
	public WheelDetail getWheelDetail(TypesOfWheel type, int width, int height,
			char tireStructure, int diameter, float load, float speed) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WheelDetail> getRandomLstWheelDetailForIndex() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<WheelDetail> lstWheelDetails = null;
		try {
			lstWheelDetails = (List<WheelDetail>) session.createQuery(
					"from WheelDetail order by NEWID()").setMaxResults(6)
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstWheelDetails;

	}

	// Phatdt start
	@SuppressWarnings("unchecked")
	public List<WheelDetail> getAllListWheelDetail() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<WheelDetail> listWheelDetailsAll = null;
		try {

			listWheelDetailsAll = (List<WheelDetail>) session.createQuery(
					"from WheelDetail").list();
			// System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listWheelDetailsAll;
	}

	// list search
	@SuppressWarnings("unchecked")
	public List<WheelDetail> getAllListWheelDetailForSearch(String wheelDetailID, String typeOfWheelID, String typeOfAutoID) {
		// TODO Auto-generated method stub
		System.out.println("DAOSearch");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<WheelDetail> listWheelDetailsAll = null;
		String SQL_QUERY = null;

		if (wheelDetailID.equals("")) {
			wheelDetailID = "%";
		}
		if (!typeOfWheelID.equals("0") && !typeOfAutoID.equals("0")) {
			SQL_QUERY = "from WheelDetail as WD where WD.wheelDetailID like:name and WD.typesOfWheel.typeOfWheelID='"
					+ typeOfWheelID
					+ "' and WD.typesOfWheel.typeOfAuto.typeOfAutoID='"
					+ typeOfAutoID + "'";
		} else {
			if (typeOfWheelID.equals("0") && typeOfAutoID.equals("0")) {
				SQL_QUERY = "from WheelDetail as WD where WD.wheelDetailID like:name";
			} else {
				if (!typeOfWheelID.equals("0") && typeOfAutoID.equals("0")) {
					SQL_QUERY = "from WheelDetail as WD where WD.wheelDetailID like:name and WD.typesOfWheel.typeOfWheelID='"
							+ typeOfWheelID + "'";
				} else {
					SQL_QUERY = "from WheelDetail as WD where WD.wheelDetailID like:name and e.typesOfWheel.typeOfAuto.typeOfAutoID='"
							+ typeOfAutoID + "'";
				}
			}
		}

		try {
			listWheelDetailsAll = (List<WheelDetail>) session.createQuery(
					SQL_QUERY).setParameter("name", "%" + wheelDetailID + "%")
					.list();
			System.out.println("DAOSearch end");

			// System.out.println("thuc hien dao!");
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return listWheelDetailsAll;
	}
	// Phatdt end

}
